function random(n) {
    var s = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    var str = "";
    for (var i = 0; i < n; i++) {
        var index = Math.floor(Math.random() * 62);
        str += s.charAt(index);
    }
    return str;

}


/*实现网页载入进去就加载出验证码**/

window.onload = function() {
    var sub = document.getElementById("submit");
    var username = document.getElementById('username');
    var MyID = document.getElementById('IDnum');
    var Myphone = document.getElementById('phone');
    var MyEmail = document.getElementById('email');
    var inputCode = document.getElementById('input-code');
    var randomcode = document.getElementById("repassw-err");
    // 加载验证码
    randomcode.innerHTML = random(4);
    randomcode.onclick = function() {
            randomcode.innerHTML = random(4);

        }
        // console.log("验证码为" + randomcode.innerHTML);
        // var code, repassword;
        // sub.onclick = function() {
        //     if (username.value) {
        //         if (MyID.value) {
        //             if (Myphone.value.length >= 11) {
        //                 if (MyEmail.value) {

    //                     if (randomcode.innerHTML.toUpperCase() == inputCode.value.toUpperCase()) {
    //                         alert("注册成功！"); //
    //                     } else alert("验证码错误");

    //                 } else alert("请输入邮箱")
    //             } else alert("请输入符合规范的电话号码")
    //         } else alert("请输入身份证号");
    //     } else alert("请输入用户名");
    // }

}

function shoeTips(spanId, tips) {
    var span = document.getElementById(spanId);
    span.innerHTML = tips;
}
/**
 * 校验用户名
 */
function hint() {

    var hint = document.getElementById("hint");
    if (username.value.length < 3) {
        hint.innerHTML = "用户名太短";
        return false;
    } else {
        hint.innerHTML = "用户名合格";
        return true;
    }
}

function hint_hide() {
    var hint = document.getElementById("hint");
    hint.innerHTML = "";
}
/**
 * 校验身份证号
 */

function checkIDnum() {
    var value = document.getElementById("IDnum").value;
    var hint = document.getElementById("IDhint");
    if (value.length < 6) {
        hint.innerHTML = "身份证号太短";
        return false;
    } else {
        hint.innerHTML = "身份证格式合格";
        return true;
    }
}

function IDnum_hide() {
    var hint = document.getElementById("IDhint");
    hint.innerHTML = "";
}
/**
 * 校验电话号码
 */
function phonehint() {
    var value = document.getElementById("phone").value;
    var hint = document.getElementById("phonehint");
    if (value.length < 11) {
        hint.innerHTML = "请输入正确的电话号码格式";
        return false;
    } else {
        hint.innerHTML = "电话号码格式合格";
        return true;
    }
}

function phone_hide() {
    var hint = document.getElementById("phonehint");
    hint.innerHTML = "";
}


/**
 * 校验邮箱
 */

function emailCheck() {
    var emailValue = document.getElementById("email").value;
    var email_hint = document.getElementById("email_hint");
    var atpos = emailValue.indexOf("@");
    var dotpos = emailValue.lastIndexOf(".");
    if (atpos < 1 || dotpos < atpos + 2 || dotpos + 2 >= emailValue.length) {
        // 输入的数据必须包含 @ 符号和点号(.)。同时，@ 不可以是邮件地址的首字符，并且 @ 之后需有至少一个点号
        email_hint.innerHTML = "邮箱格式不合格";
        return false;
    } else {
        email_hint.innerHTML = "邮箱格式合格";
        return true;
    }

}

function emailHide() {
    var email_hint = document.getElementById("email_hint");
    email_hint.innerHTML = "";
}
/***
 * 校验验证码是否合适
 */
function checkcode() {
    var codehint = document.getElementById("codehint");
    var randomcode = document.getElementById("repassw-err");
    var inputCode = document.getElementById('input-code');
    if (randomcode.innerHTML.toUpperCase() == inputCode.value.toUpperCase()) {
        codehint.innerHTML = " ";
        return true;
    } else {

        codehint.innerHTML = "验证码不正确";
        return false;
    }
}

function code_hide() {
    var codehint = document.getElementById("codehint");
    codehint.innerHTML = "";
}


function checkForm() {
    var flag = hint() && checkIDnum() && phonehint() && emailCheck() && checkcode();
    return flag;
}
// username.onblur = hint_hide();
// username.onkeyup = hint();
// username.onfocus = shoeTips('hint', '用户名长度不能小于三');

// MyID.onblur = IDnum_hide();
// MyID.onkeyup = checkIDnum();
// MyID.onfocus = shoeTips('IDhint', '输入正确的身份证号');

// Myphone.onblur = phone_hide();
// Myphone.onkeyup = phonehint();
// Myphone.onfocus = shoeTips('phonehint', '请输入正确的电话号码');

// MyEmail.onblur = emailHide();
// MyEmail.onkeyup = emailCheck();
// MyEmail.onfocus = shoeTips('email_hint', '邮箱必须包含@.');

// inputCode.onblur = code_hide();
// inputCode.onkeyup = checkcode();
// inputCode.onfocus = shoeTips('codehint', '请输入正确的验证码');